package it.yuzuojian.com.mapper;

import it.yuzuojian.com.javaBean.Tags;
import it.yuzuojian.com.javaBean.Type;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;
@Mapper
@Repository
public interface TagsMapper {
    @Select("select * from tags ")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "name",property = "name"),
            @Result(column = "count",property = "count"),
            @Result(column = "id",property = "articles",
                    many = @Many(select = "it.yuzuojian.com.mapper.ArticleMapper.findByTagId", fetchType = FetchType.LAZY))
    }
    )
    List<Tags> findAll();
    @Insert("insert into tags values(null,#{name},#{count})")
    void add(Tags tags);
    @Select("select * from tags where id=#{id}")
    Tags findById(Integer id);
    @Update("update tags set name=#{name} where id=#{id}")
    void update(Tags tags);
    @Delete("delete from tags where id=#{id}")
    void deleteById(Integer id);
    //@Select("select tagId from articleTags where articleId=#{id}")
    @Select("select * from tags where id in (select tagId from articletags where articleId=#{articleId})")
    Tags findByIdInArticleTags(Integer articleId);

    @Select("SELECT  *  FROM tags  ORDER BY count desc limit 0,3")
    List<Tags> findAllCount();
    @Update("update tags set count=count+1 where id=#{tagId}")
    void addCount(Integer tagId);
    @Update("update tags set count=count-1 where id=#{tagId}")
    void deleteCount(Integer tagId);
    @Select("select count(1) from tags")
    Integer findCounts();


}